import Common from '../../common/common';
let common = new Common;
const app = getApp()
Page({
  data: {
    liveActivityList: '', //
    preSaleActList: [], //预售活动列表
    recordMain: [{
        title: "进行中",
        status: 1
      },
      {
        title: "已过期",
        status: 3
      },
    ],
    status: 1,
    tabContent: [],
    tabContentLength: 0,
    currentTab: 0,
    navScrollLeft: 0,
    winWidth: 0,
    winHeight: 0,
    pageNumber: 1,
    page: '1',
    size: 15 //分页默认10条
  },
  // 活动详情页
  activeDetail: function (e) {
    let uuid = e.currentTarget.dataset.uuid
    let type = e.currentTarget.dataset.type
    wx.navigateTo({
      url: './activeDetail/activeDetail?uuid=' + uuid + '&type=' + type,
    })
  },
  // 
  onShow: function () {
    var that = this;
    that.setData({
      page: 1
    })
    that.getActiveList(1, 1); //调用活动管理列表信息接口
    this.getPreSaleAct(); //预售活动列表
    //直播活动入口  
    this.getLiveActivity();
  },
  // 事件处理函数
  onLoad: function () {
    var that = this;
    /** 获取系统信息*/
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight,
        });
      }
    });

  },
  // 获取活动管理列表信息
  getActiveList(page, type) {
    var that = this;
    wx.showLoading({
      title: '加载中',
    })
    // let status = this.data.currentTab;
    // if (!status) {
    //   status = 4
    // }
    var data = {
      "size": that.data.size,
      "current": that.data.page, //页码
      "condition": {
        "orgSeq": app.globalData.orgSeq, //orgSeq 门店id
        "status": this.data.status // 查询方式 4-全部 1-进行中 2-已参加 3-已过期
      },
      "ascs": [],
      "descs": []
    }
    common.requestAjax('/api/goods-service/v1/goodsShopActivity/queryGoodsActivityList', 'POST', data).then(res => {
      if (res.data.code == 1) {
        setTimeout(() => {
          wx.hideLoading()
        }, 500)
        let arraylist = this.data.tabContent;
        if (type == 1) {
          //下拉刷新
          arraylist = res.data.records;
        } else if (type == 2) {
          //上拉加载
          arraylist = arraylist.concat(res.data.records);
          setTimeout(() => {
            wx.hideLoading()
          }, 500)
        }
        this.setData({
          tabContent: arraylist,
          tabContentLength: arraylist.length,
        })
        arraylist.forEach((item) => {
          item.beginTime = item.beginTime.substr(0, 10) //时间转化  
          item.endTime = item.endTime.substr(0, 10) //时间转化  
        });
        this.setData({
          tabContent: arraylist,
        })
      } else {
        common.showToast(res.data.message);
      }
    }).catch(err => {
      common.showToast(err);
    })
  },
  // 下拉刷新
  onPullDownRefresh: function () {
    //清空搜索框
    this.setData({
      pageNumber: 1,
    })
    this.getActiveList(1, 1)
    setTimeout(() => {
      wx.stopPullDownRefresh();
    }, 1200)
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    that.setData({
      page: Number(that.data.page) + 1
    })
    this.getActiveList(1, 2) //调用活动管理列表信息接
    wx.showLoading({
      title: '正在加载中...',
    })
    console.log(this.data.page)
  },

  // 滑动事件
  // 点击标题切换当前页时改变样式
  switchNav: function (e) {
    var that = this
    var cur = e.currentTarget.dataset.current;
    let status = e.currentTarget.dataset.status
    // 数据列表置空
    that.setData({
      tabContent: [],
      page: 1
    })
    if (that.data.currentTab == cur) {
      return false;
    } else {
      that.setData({
        currentTab: cur,
        status: status,
      })
    }
    that.getActiveList(1, 1) //调用活动管理列表信息接口  
  },
  getPreSaleAct() {
    common.requestAjax("/api/flashsale-service/v1/presale/getPreSaleActList", "POST", {
      "orgSeq": app.globalData.orgSeq, //orgSeq 门店id
    }).then(res => {
      if (res.data.code) {
        let arrayList = res.data.records || [];
        let preSaleActList = [];
        let acPreSaleActList = [];
        arrayList.forEach(item=>{
          if(item.publishStatus==1&&item.actStatus==0){
            //已发布且活动未过期
            acPreSaleActList.push(item)
          }else if(item.actStatus==1&&item.joinStatus==1){
            //已参加且活动过期的
            preSaleActList.push(item)
          }
        })
        this.setData({
          preSaleActList,
          acPreSaleActList
        })
      }
    })
  },
  //预售活动详情
  toDetail(e) {
    let item = e.currentTarget.dataset.item;
    getApp().globalData.item = item
    wx.navigateTo({
      url: '/pages/activityMgt/preSaleActivity/index?id=' + item.uuid,
    })
  },
  /**
   * 获取直播活动详情
   * 店长且有活动的情况下开放入口
   * **/
  getLiveActivity() {
    common.requestAjax("/api/flashsale-service/v1/zbRelatedApi/planByOrgSeq", "GET", {
      orgSeq: app.globalData.orgSeq
    }).then(res => {
      let acLiveActivityList = [];
      let liveActivityList = [];
      let arrayList = res.data.result||[];
      arrayList.forEach(item=>{
        if(item.runningStatus!=1&&item.publishStatus==1){
          //runningStatus1 已结束 2进行中 3未开始  publishStatus 1已发布
          acLiveActivityList.push(item)
        }else if(item.runningStatus==1&&item.approvalStatus!=0){
          // 已过期的内容 只展示活动结束且该店已参加的数据
          liveActivityList.push(item)
        }
      })
      this.setData({
        liveActivityList,
        acLiveActivityList
      })
    })
  },
  //直播活动
  toLiveDetail(e) {
    let item =  e.currentTarget.dataset.item;
    if(item.runningStatus==1){
      getApp().globalData.liveActivityInfo = item;
      wx.navigateTo({
        url: '/pages/myCenter/liveActivity/index?isEnd=1',
      })
    }else{
      wx.navigateTo({
        url: '/pages/myCenter/liveActivity/index?isEnd=0',
      })
    }
  }
})